#version 330 core
out vec4 FragColor;  // 输出片段颜色

in vec3 uvw;
in vec2 TexCoord;

uniform samplerCube cube;
uniform sampler2D sunTexture;//太阳纹理

void main()
{
    vec3 skyColor = vec3(0.39608, 0.60392, 0.69020);
    float miePhase = 1.0 / (4.0 * 3.14159);  // 简化Mie散射相位函数
    float rayleighPhase = 3.0 / (16.0 * 3.14159) * (1.0 + uvw.y * uvw.y);  // Rayleigh散射
    float scattering = rayleighPhase * 0.8 + miePhase * 0.2;  // 混合散射

    // 根据高度角调整散射强度
    float heightFactor = pow(1.0 - abs(uvw.y), 4.0);  // 靠近地平线时散射更强
    vec3 scatteredColor = skyColor * scattering * heightFactor * 7.5;

    vec3 envColor = vec3(0.52941, 0.80784, 0.92157);
    vec3 finalColor = envColor + scatteredColor;
    //FragColor = vec4(envColor + scatteredColor, 1.0);

    // 计算当前片段方向与太阳方向的夹角
    float sunApparentSize = 0.6;
    vec3 sunDirection = normalize(vec3(0.0, 1.0, 0.0));
    float sunDot = dot(normalize(uvw), sunDirection);
    float sunAngle = acos(sunDot);


    vec3 sunPosition = normalize(vec3(0, 0, 50));
    vec2 sunOffset = vec2(0, -0.35);
    float intensity = 2.0;
    //float distance = length(sunPosition - uvw);
    float sunSize = 0.475 * 0.975;
    float innerSize = sunSize * 1.02;
    if(abs(TexCoord.x + sunOffset.x - sunPosition.x) > sunSize && abs(TexCoord.x + sunOffset.x - sunPosition.x) < 1 - sunSize &&
        abs(TexCoord.y + sunOffset.y - sunPosition.y) > sunSize && abs(TexCoord.y + sunOffset.y - sunPosition.y) < 1 - sunSize){

        if(abs(TexCoord.x + sunOffset.x - sunPosition.x) > innerSize && abs(TexCoord.x + sunOffset.x - sunPosition.x) < 1 - innerSize &&
        abs(TexCoord.y + sunOffset.y - sunPosition.y) > innerSize && abs(TexCoord.y + sunOffset.y - sunPosition.y) < 1 - innerSize){
            finalColor = vec3(1.0, 1.0, 0.9);
        }
        else{
            finalColor = vec3(1.0 * intensity, 0.843137 * intensity, 0.0);
        }
    }

//    float saturation = 0.75;// 饱和度
//    float luminance = dot(finalColor.rgb, vec3(0.299, 0.587, 0.114));
//    finalColor = mix(vec3(luminance), finalColor.rgb, saturation);

    FragColor = vec4(finalColor, 1.0);
}